<?php 
# $Id$
require_once(APP_LIB_DIR . 'Sajax.php');
require_once('utils.inc.php');

function getlist($p='',$search='',$by='1',$s='DESC') { 
    $a = new GroupManagementAction();
    parse_str($search,$res);
    return $a->getlist($p,$res,$by,$s); 
}
function lookup($what='',$p1=''){
    $l = new GroupManagementAction();
    return $l->lookup($what,$p1);
}
function save($detail='',$id='',$action=''){
    $s = new GroupManagementAction();
    parse_str($detail,$res);
    return $s->save($res,$id,$action);
}
function actions($action='',$p1='',$p2='',$p3=''){
    $a = new GroupManagementAction();
    return $a->actions($action,$p1,$p2,$p3);
}

sajax_init();
//$sajax_debug_mode = 1;
sajax_export('getlist');
sajax_export('lookup');
sajax_export('save');
sajax_export('actions');
sajax_handle_client_request();
$sajax_request_type = 'POST';
require_once('header.php'); 
?>
<script language="JavaScript" type="text/javascript">

<?php sajax_show_javascript();?>

var s = "ASC";
var b = 1;

function page(p) {
    st = $('form1').toQueryString();
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function sort(by){
    s = (s=="DESC") ? "ASC" : "DESC";
    st = $('form1').toQueryString();
    p = $('pagenum');
    p = (p) ? p.value : 1;
    b = by;
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function filter(){
    st = $('form1').toQueryString();
    p = 1
    //st = Json.toString(st);
    load();
    x_getlist(p,st,b,s,getlist_cb);
}
function getlist_cb (new_data){
    //alert(new_data)
    data = Json_eval(new_data);
    var fields = [ ["id","ID"],
                   ["name","Group Name"],
                   ["description","Description"]
                 ]
    var  extra = [ [true,'id','edit'],
                   [true,'id','del']
                 ]
                 
    $("thetable").innerHTML = data[1]
    $("thetable").innerHTML += mytable(data[0],fields, extra)
}

function members_cb (gid){
//alert("asdf" + gid);
    x_actions('get_members',gid,function(nd){
        d = Json_eval(nd);
        d.each(function(v){
          alert("members:"+ (Json_eval(v))); 
        });
    });
}
function save (what) {
if (what=='form2'){
    data = $('form2').toQueryString();
    id = $('myid').value
    $('myid').value = ''
    x_save(data,id, function(new_data1){
    Json_eval(new_data1);
    if (id){
      alert("You have successfully updated the Group Information");
    }else{
      alert("You have successfully save the Group Information");
    }
      filter();
      show_t()
    });
//Saving/Updating List of Members for spesific group
}else{
    data = $('form3').toQueryString();
    id = $('myid').value
    $('myid').value = ''
    gm=byID('group_members').value=getIt(byID('members'));
   
     x_actions('save_gm',gm,id,function(nd){
    Json_eval(nd);
      if (id){
        alert("You have successfully save Group Member List");
      }else{
        alert("You have successfully save the Group Member List");
      }
        filter();
        show_t()
    });
}
}

function show_t(){
    if($('thetable').style.display == "none"){
        hide_t('newform','thetable')
        $('altfrm').value = "New Group"
    } else {
        hide_t('thetable','newform')
        $('altfrm').value = "Back to List"
    }
    $('form2').reset();
    $('form3').reset();
}

function clearForm(){
  data = $('form2').toQueryString();
  $('myid').value=''
         ClearOptions($('members'))
         ClearOptions($('memberlist'))

}
function edit (id) {
    x_save('',id,'get', function(new_data1){
       d = Json_eval(new_data1);
       $('name').value = d[0]['name'];
       $('description').value = d[0]['description'];
       $('myid').value = d[0]['id'];
//       $('group_members').value = d[0]['id'];
       //optionSelected($('level'), d[0]['level_id'])

       hide_t('thetable','newform');
    }) 
    
//This will generate the list of contacts where is a member of a group
//START
	x_actions('get_members',id,function(nd){
        m = Json_eval(nd);
         ClearOptions($('members'))
         AddToOptionList(m, $('members'), 'id','name')
    });
///END
//This will generate the list of contacts where is not a member of a group
//START
	x_actions('get_memberslist',id,function(nd){
        ml = Json_eval(nd);
         ClearOptions($('memberlist'))
         AddToOptionList(ml, $('memberlist'), 'id','name')
    });
///END

    return false;
}

function del (id) {
    if (confirm('Are you sure you want to delete this Record?')){
      x_save('',id,'del', function(new_data1){
        Json_eval(new_data1);
        filter();
      })   
    }
}

function load (){
    //$("thetable").empty().addClass('ajax-loading');
    $("thetable").innerHTML = loading;
}

</script>
<script type="text/javascript" src="<?=WEB_MODULE_DIR?>js/javascript.js"></script>
<div id="toolbar-box">
  <div class="m">
    <div class="toolbar-list" id="toolbar">
      <!-- TOOL BAR MENU ex:HELP|NEW|EDIT-->
      <ul>
      <!--  <li class="button" id="toolbar-cancel"><a href="#" onclick="show_t()" class="toolbar"><span class="icon-32-cancel"></span>Cancel</a></li>-->
      </ul>
      <!-- END TOOL BAR MENU -->
      <div class="clr"></div>
    </div>
    <div class="pagetitle icon-48-cpanel"><h2><?=$config['version']?></h2></div>
     <!-- [</?=strtoupper($auth['privilege'])?>]-->
  </div>
</div>
<div>
 <span><h2>&nbsp;&nbsp;&nbsp;::GROUP MANAGEMENT::</h2><span>
</div>
<div id="submenu-box">
  <div class="m">
    <div class="width-100 fltlft">
      <fieldset class="adminform">
        <legend><a href="javascript: show_tool('panel');">:: SEARCH PANEL</a></legend>
       <!-- filter panel -->
        <div id="panel">
          <form id="form1" class="wufo" action="" method="post" onSubmit="filter();return false;">
            <span><label class="desc">Seach Contact:</label></span>
            <select id="jform_params_helpsite" name="search[searchfield]">
	      <option value="name" selected="selected">-Use Default-</option>
              <option value="name">Group Name</option>
              <option value="description">Desctription</option>
            </select>
            <input type="text" name="search[searchtext]" value="" class="inputbox required" size="30"onKeyup="filter()"/>
            <button id="saveForm" class="btn" onClick="filter()">Search</button>
            <button class="btn" onClick="this.form.reset();filter()">Clear</button>
            <button id="altfrm" class="btn" type="button" value="New Group" onClick="show_t()"/>New Group</button>
          </form>
        </div>
      </fieldset>
      <!-- filter panel end -->
    </div>
    <div class="clr"></div>
  </div>
</div>
<div>
 <span><h2>&nbsp;&nbsp;&nbsp;::SEARCH RESULT::</h2><span>
</div>
<div id="element-box">
<form id="form2" method="post" onSubmit="return false;">
  <div class="m">
    <div id="thetable"><img src="/mcs/modpub/images/loading_small.gif"/></div>
    <div id="newform" style="display: none" align="center">
    <input type="hidden" value="" id="myid" />
      <div class="width-25 fltlft">
        <fieldset class="adminform">
        <legend>Group Information</legend>
          <ul>
            <li><span><label class="desc">Group Name</span></label>
              <input type="text" name="details[name]" id="name" value="" class="inputbox" size="35"/>
            </li>
            <li><span><label class="desc">Description</span></label>
              <input type="text" name="details[description]" id="description" value="" class="inputbox" size="35"/>
            </li>
            <li>
              <button id="saveForm" class="btn" type="button" value="Save" onClick="save('form2');"/>Save</button>
              <button id="saveForm" class="btn" type="reset" value="Clear" onClick="this.form.reset();"/>Clear</button>
              <button id="saveForm" class="btn" type="button" value="Cancel" onClick="this.form.reset();clearForm();hide_t('newform', 'thetable')"/>Cancel</button>
            </li>
          </ul>
        </fieldset>
      </div>
      </form>
      <div class="width-75 fltrt">
        <div>
          <div style="display:none;"><div>
        </div>
      </div>
      <div class="width-20 fltlft">
      <form id="form3" method="post" onSubmit="return false;">
        <input type="hidden"  value="" id="group_members" />
        <fieldset class="adminform">
          <legend>Group Assignment</legend>
          <ul>
            <li>
              <table>
                <tr>
                  <th>Available Members</th>
                  <th>&nbsp;</th>
                  <th>Members</th>
                </tr>
                <tr>
                  <td> 
                    <select  id="memberlist" multiple="multiple"  name="memberlist" style="height:300px;width:400px;"></select>
                  </td>
                  <td>
                    <button class=btn" onclick="addIt2(byID('memberlist'),byID('members'))"> >></button>
                    <button class="btn" onclick="delIt2(byID('memberlist'),byID('members'))"><<</button>
                  </td>
                  <td>
                    <select id="members" multiple="multiple"  name="members" style="height:300px;width:400px;"></select>
                  </td> 
                </tr>
              </table>
            </li>
          </ul>
        <ul>
          <li>
            <button id="saveForm" class="btn" type="button" value="Save" onClick="save('form3');"/>Save</button>
            <button id="saveForm" class="btTxt" type="reset" value="Clear" onClick="this.form.reset();"/>Clear</button>
            <button id="saveForm" class="btTxt" type="button" value="Cancel" onClick="this.form.reset();clearForm();hide_t('newform', 'thetable')"/>Cancel</button>
          </li>
        </ul>
        </fieldset>
        
      </form>
      </div>
    </div>
    </div>
    <div class="clr"></div>
  </div>
</div>
</div>
</div>
</br>
<script type="text/javascript">
x_getlist(getlist_cb)
</script>
<?php require('footer.php'); ?>
</body>
</html>
